package DataLayer;

import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JOptionPane;

import Model.User;

public class DaoUser extends DaoConnection {
	
	private static DaoUser single;
	public String dbPassword = "";
	public String dbEmailAdres = "";

	
	/*public String checkEmail(User G){
		try{ 
			openConn();
			Statement stmt = getConn().createStatement();
			//System.out.println("SELECT * FROM deb42181_bb.User WHERE email ='" + G.getemailAdres() + "'");
			String queryCheck = "SELECT * FROM deb42181_bb.User WHERE email ='" + G.getemailAdres() + "'";
			System.out.println(queryCheck);
			stmt.execute(queryCheck);
			stmt.close();
			closeconn();
			System.out.println(checkEmail(G));
			if (queryCheck == G.getemailAdres()){
				JOptionPane
				.showMessageDialog(null,
						"Dit e-mail adres is al in gebruik",
						null, JOptionPane.PLAIN_MESSAGE, null);
				}
			else {
				
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		//? return type
		return dbEmailAdres;
	}
		*/

	


	public void insertUser(User G) {
		try {
			openConn();
			Statement stmt = getConn().createStatement();
			String sql = "insert into User(firstname,insertion,lastname,adres,zipcode,city,email,phonenumber," +
					"password,subscription,registrationDate) values('"
					+ G.getFirstName()
					+ "','"
					+ G.getinsertion()
					+ "','"
					+ G.getsurename()
					+ "','"
					+ G.getadres()
					+ "','"
					+ G.getzipcode()
					+ "','"
					+ G.getCity()
					+ "','"
					+ G.getemailAdres()
					+ "', '"
					+ G.getphonenumber()
					+ "','"
					+ G.getpassword()
					+ "','"
					+ G.getAbonnement()
					+ "','"
					+G.getregistratieDatum()
					+"')";
			System.out.println(sql);
			stmt.execute(sql);
			stmt.close();
			closeconn();
		}catch (Exception sql){
			JOptionPane
			.showMessageDialog(null,
					"Dit e-mail adres is al in gebruik",
					null, JOptionPane.PLAIN_MESSAGE, null);
						
		//}	
		// catch (Exception e) {
			// TODO: handle exception
		}
		return;
	}
	
	

	public void loginCheck(User g){
		 
		 try {
			openConn();
			Statement stmt = getConn().createStatement();
			String sql = "SELECT * FROM User WHERE email ='"
					+ g.getemailAdres() + "' && password ='"
					+ g.getpassword() + "'";
			
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				System.out.println("Login check");
				g = new User();
				g.setFirstName(rs.getString(1));
				g.setinsertion(rs.getString(2));
				g.setsurename(rs.getString(3));
				g.setadres(rs.getString(4));
				g.setzipcode(rs.getString(5));
				g.setCity(rs.getString(6));
				dbEmailAdres = rs.getString(7);
				dbPassword = rs.getString(9);
				g.setphonenumber(rs.getString(8));
				g.setAbonnement(rs.getInt(10));
				System.out.println("Genius");
				System.out.println(dbEmailAdres);
				System.out.println(dbPassword);
				System.out.println(g);
			}
			stmt.close();
			closeconn();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	 }
	
	 public void setLogedInUser(){
		 
	 }
	
	public static DaoUser getInstance(){
		if(single == null){
			single = new DaoUser();
		}
		return single;
	}
	
	public static void selfDestroy(){
		single = null;
	}

	public String getDbPassword() {
		return dbPassword;
	}

	public void setDbPassword(String dbPassword) {
		this.dbPassword = dbPassword;
	}

	public String getDbEmailAdres() {
		return dbEmailAdres;
	}

	public void setDbEmailAdres(String dbEmailAdres) {
		this.dbEmailAdres = dbEmailAdres;
	}

}
